local Class = require("Core.Class")
local LuaBehaviour = require("Core.LuaBehaviour")
local UnityUtils = require("Game.Util.UnityUtils")
local Coroutine = require("Core.Coroutine")

--- @class BulletBase
local BulletBase = Class("BulletBase", LuaBehaviour)

function BulletBase:__init()
    -- 常量
    self.IS_BLAST_ID = CSE.Animator.StringToHash('IsBlast')

    -- 基本属性
    self.bulletSpeed = 20

    -- 当前状态
    self.isBlast = false

    -- 组件
    self.anim = self.gameObject:GetComponent('Animator')
    self.boxColl = self.gameObject:GetComponent('BoxCollider2D')
end

function BulletBase:OnEnable()
    -- 如果子弹存在超过3秒就销毁
    local waitThreeSecondDestroy = Coroutine.Create(bind(UnityUtils.WaitFormSecond), 3, function ()
        CSE.Object.Destroy(self.gameObject)
    end)
    self.host:StartCoroutine(waitThreeSecondDestroy)
end

function BulletBase:FixedUpdate()
    self:Movement()
end

function BulletBase:OnTriggerEnter2D(other)
    self:OnBlast()
end

function BulletBase:Movement()
    if self.isBlast then
        return
    end

    self.transform.position = self.transform.position + self.transform.up * self.bulletSpeed * CSE.Time.fixedDeltaTime
end

function BulletBase:OnBlast()
    self.isBlast = true
    self.boxColl.enabled = false

    self.anim:SetBool(self.IS_BLAST_ID, true)
    local waitAnimEndCoroutine = Coroutine.Create(bind(UnityUtils.WaitFormSecond), 0.3, function ()
        CSE.Object.Destroy(self.gameObject)
    end)
    self.host:StartCoroutine(waitAnimEndCoroutine)
end

return BulletBase